package com.google.code.mochaccino.framework.rest;/*
 * Copyright 2012 Claude Houle claude.houle@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import com.google.code.mochaccino.framework.rest.mime.MimeTypes;
import com.google.code.mochaccino.framework.rest.resources.ResourceService;
import java.net.URI;
import java.net.URISyntaxException;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;

/**
 * The purpose of this class is to server resources from the root of the domain and
 * redirect to the proper 'first' resource. All CSS, JS, Images will be served from here.
 */
@Path( "/" )
public class Root {
	/** The Resource Service */
	private ResourceService resourceService;

	/** The Actual Redirect Path */
	private String redirect = null;

	/* Constants for Paths & Names for IoC */
	public static final String NAME_ROOT_REDIRECT_PATH = "rootRedirectPath";
	public static final String PATH_CSS                = "/css";
	public static final String PATH_FAVICON            = "/favicon.ico";
	public static final String PATH_IMAGE              = "/img";
	public static final String PATH_JS                 = "/js";
	public static final String PATH_LIBS               = "/libs";
	public static final String PATH_LOGOUT             = "/logout";

	/** Constructor */
	@Inject
	public Root( @Named( NAME_ROOT_REDIRECT_PATH ) String redirect, ResourceService service ) {
		this.resourceService = service;
		this.redirect = redirect;
	}

	@Path( PATH_CSS + "/{id:.*}" )
	@GET
	@Produces( MediaType.WILDCARD )
	public Response css( @Context Request request, @PathParam( "id" ) String id ) {
		return resourceService.getResource( request, PATH_CSS + "/" + id );
	}

	@Path( PATH_FAVICON )
	@GET
	@Produces( MimeTypes.TYPE_ICO )
	public Response icon( @Context Request request ) throws URISyntaxException {
		return resourceService.getResource( request, PATH_FAVICON );
	}

	@Path( PATH_IMAGE + "/{id:.*}" )
	@GET
	@Produces( MediaType.WILDCARD )
	public Response image( @Context Request request, @PathParam( "id" ) String id ) {
		return resourceService.getResource( request, PATH_IMAGE + "/" + id );
	}

	@Path( PATH_JS + "/{id:.*}" )
	@GET
	@Produces( MediaType.WILDCARD )
	public Response javascript( @Context Request request, @PathParam( "id" ) String id ) {
		return resourceService.getResource( request, PATH_JS + "/" + id );
	}

	@Path( PATH_LIBS + "/{id:.*}" )
	@GET
	@Produces( MediaType.WILDCARD )
	public Response libs( @Context Request request, @PathParam( "id" ) String id ) {
		return resourceService.getResource( request, PATH_LIBS + "/" + id );
	}

	@Path( PATH_LOGOUT )
	@GET
	@Consumes( MediaType.WILDCARD )
	@Produces( MediaType.WILDCARD )
	public Response logout( @Context HttpServletRequest request ) {
		HttpSession session = request.getSession(false);
		if ( session != null ) {
			session.invalidate();
		}
		return RedirectionUtil.redirect( "/" ).build();
	}

	@GET
	@Produces( MediaType.TEXT_HTML )
	public Response root() throws URISyntaxException {
		return Response.temporaryRedirect( new URI( redirect ) ).build();
	}
}
